package com.wbj.direction.sensor.direction

import android.content.Context
import android.graphics.*
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.recyclerview.widget.LinearSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.noboauto.vehicle.view.util.AltitudeUtil
import com.noboauto.vehicle.view.util.AltitudeUtil.dipToPx
import com.noboauto.vehicle.view.util.AltitudeUtil.sp2px
import com.wbj.direction.R
import kotlin.math.abs

/**
 * @auth: WANGCHUANGJUN
 * @mail: wangchuangjun@noboauto.com
 * @desc:
 * @date: 2022/2/16 10:22
 */
class ShadowDirectionView:DirectionView {

    var snapHelper:LinearSnapHelper? =null
    var centerView: TextView? = null
    var mFadingEdgeLength:Int = 400

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ){
        val ta = getContext().obtainStyledAttributes(
            attrs, R.styleable.ShadowDirectionView, defStyleAttr, 0)
        ta.let {
            mFadingEdgeLength = ta.getDimensionPixelSize(R.styleable.ShadowDirectionView_fadeRecyclerViewLength,400)
        }
        recyclerView.isHorizontalFadingEdgeEnabled = true
        recyclerView.setFadingEdgeLength(mFadingEdgeLength)
        snapHelper = LinearSnapHelper()
//        snapHelper?.attachToRecyclerView(recyclerView)
        isNeedParentDraw = false
    }


    fun setRecyclerViewFadingLength(length: Int) {
        recyclerView.setFadingEdgeLength(length)
        invalidate()
        postInvalidate()
    }

    override fun onSensorChanged(event: SensorEvent?) {
        if (System.currentTimeMillis() - lastUpdateTime < 200) return

        lastUpdateTime = System.currentTimeMillis()
        var sensorType = event?.sensor?.type
        if (sensorType == Sensor.TYPE_ORIENTATION) {
            var degree = event?.values?.get(0)?.minus((mWidth-itemWidth)*15/itemWidth/2)
            Log.i(TAG, "onSensorChanged:currentDegree: ${currentDegree},degree:${event?.values?.get(0)}")
            if (degree != null && abs((degree - currentDegree)) >= 1) {
                when {
                    degree - currentDegree > 180 -> {
                        recyclerView.smoothScrollBy((((degree - 360 - currentDegree) / 15f) * itemWidth ).toInt(),
                            0)
                    }
                    currentDegree - degree > 180 -> {
                        recyclerView.smoothScrollBy((((degree + 360 - currentDegree) / 15f) * itemWidth).toInt(),
                            0)
                    }
                    else -> {
                        recyclerView.smoothScrollBy((((degree - currentDegree) / 15f) * itemWidth).toInt(),
                            0)
                    }
                }
                currentDegree = degree
                currentDegreeShow = event?.values?.get(0)!!
                invalidate()
                postInvalidate()
            }
        }
    }

    override fun dispatchDraw(canvas: Canvas) {
        super.dispatchDraw(canvas)
        dropDownDrawable.setBounds(mWidth.shr(1) - dipToPx(context,7.5f),
            0,
            mWidth.shr(1) + dipToPx(context,7.5f),
            dipToPx(context,7.5f))
        dropDownDrawable.draw(canvas)
        if (centerView != null){
            centerView?.setTextColor(Color.parseColor("#FFFEFF"))
        }
        centerView = snapHelper?.findSnapView(layoutManager) as TextView?
        centerView?.setTextColor(Color.parseColor("#4BFEC7"))
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

    }
}